package com.hyty.cordova.util;

import android.app.Activity;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.hyty.cordova.imagepicker.Album;
import com.hyty.cordova.imagepicker.AlbumConfig;
import com.hyty.cordova.imagepicker.GlideAlbumLoader;
import com.hyty.cordova.plugins.bean.Config;

import java.io.File;
import java.util.Locale;

import static com.hyty.cordova.plugins.bean.Config.CAMERA_FILE_PATH;
import static com.hyty.cordova.plugins.bean.Config.FILE_SAVED_PATH;

/**
 * ================================================================
 * 创建时间：2017/12/18 13:51
 * 创建人：赵文贇
 * 文件描述：库用到的工具
 * 看淡身边的虚伪，静心宁神做好自己。路那么长，无愧走好每一步。
 * ================================================================
 */
public final class Utils {

    public static Config CheckParams(String params) {
        try {
            Config mConfig = new Gson().fromJson(params, Config.class);
            return mConfig;
        } catch (JsonSyntaxException mE) {
            Log.e("传入参数解析出错," + mE.toString());
            return null;
        }
    }

    /**
     * 初始化存储目录，不存在则创建
     */
    public static void initFileSystem(String mFolderName) {
        File saveFilesDir = new File(FILE_SAVED_PATH + mFolderName);
        File cameraDir = new File(CAMERA_FILE_PATH);

        if (!saveFilesDir.exists())
            saveFilesDir.mkdir();

        if (!cameraDir.exists())
            cameraDir.mkdir();

        Log.d("相册目录初始化成功:"+ cameraDir.getPath());
        Log.d("文件存储目录初始化成功:"+ saveFilesDir.getPath());
    }

    public static void initImageLoder(Activity mActivity) {
        Album.initialize(
                AlbumConfig.newBuilder(mActivity.getApplication())
                        .setAlbumLoader(new GlideAlbumLoader()) // This is not necessary.
                        .setLocale(Locale.getDefault())
                        .build());
    }
}
